#ifndef CHARTWIDGET_H
#define CHARTWIDGET_H

#include <QApplication>
#include <QComboBox>
#include <QDateTime>
#include <QHBoxLayout>
#include <QLabel>
#include <QMouseEvent>
#include <QPushButton>
#include <QVBoxLayout>
#include <QWidget>
#include <QtCharts/QChart>
#include <QtCharts/QChartView>
#include <QtCharts/QDateTimeAxis>
#include <QtCharts/QLineSeries>
#include <QtCharts/QValueAxis>

QT_CHARTS_USE_NAMESPACE

enum class ChartDisplayMode {
    Embedded,  // 嵌入模式 - records页面使用，显示设备选择框
    Window     // 窗口模式 - CardTemplate使用，隐藏设备选择框，固定设备
};

// 自定义ChartView类，用于处理十字光标
class CustomChartView : public QChartView
{
    Q_OBJECT
public:
    explicit CustomChartView(QChart *chart, QWidget *parent = nullptr);
    void setCrosshairEnabled(bool enabled) { crosshairEnabled = enabled; }
    void setDataPoints(const QList<QPointF> *points) { dataPoints = points; }
    void setLineSeries(QLineSeries *lineSeries) { series = lineSeries; }

protected:
    void mouseMoveEvent(QMouseEvent *event) override;
    void mousePressEvent(QMouseEvent *event) override;
    void leaveEvent(QEvent *event) override;
    void paintEvent(QPaintEvent *event) override;

signals:
    void crosshairMoved(const QPointF &chartPoint, const QPointF &actualDataPoint);

private:
    QPointF findNearestDataPointByX(double xValue);
    void updateCrosshair(const QPoint &position);

    bool crosshairEnabled;
    QPoint crosshairPosition;
    bool crosshairVisible;
    const QList<QPointF> *dataPoints;
    QLineSeries *series;
    QPointF lastNearestPoint;  // 缓存上次找到的最近点，避免重复计算
};

class ChartWidget : public QWidget
{
    Q_OBJECT

public:
    explicit ChartWidget(QWidget *parent = nullptr,
                         ChartDisplayMode mode = ChartDisplayMode::Embedded,
                         const QString &deviceId = "");
    ~ChartWidget();

signals:
    void backToTableRequested();

private slots:
    void onDeviceSelectionChanged();
    void onPreviousClicked();
    void onNextClicked();
    void onZoomInClicked();
    void onZoomOutClicked();
    void onBackButtonClicked();
    void onCrosshairMoved(const QPointF &chartPoint, const QPointF &actualDataPoint);

private:
    void setupUI();
    void loadDeviceNames();
    void updateChart();
    void loadChartData(const QString &deviceName);
    void updateTimeRange();
    void applyThemeColors();  // 根据当前主题应用图表颜色

    QVBoxLayout *mainLayout;
    QHBoxLayout *controlLayout;
    QLabel *deviceLabel;
    QComboBox *deviceComboBox;
    QPushButton *backButton;

    // 导航控制按钮
    QPushButton *previousButton;
    QPushButton *nextButton;
    QPushButton *zoomInButton;
    QPushButton *zoomOutButton;
    QLabel *timeRangeLabel;
    QLabel *dataInfoLabel;  // 固定显示数据信息的标签

    QChart *chart;
    CustomChartView *chartView;
    QLineSeries *series;
    QValueAxis *axisY;
    QDateTimeAxis *axisX;

    // 数据管理
    QList<QPointF> allDataPoints;      // 存储所有数据点
    QList<QPointF> visibleDataPoints;  // 存储当前显示范围内的实际数据点
    QDateTime dataStartTime;           // 数据开始时间
    QDateTime dataEndTime;             // 数据结束时间
    QDateTime currentStartTime;        // 当前显示开始时间
    QDateTime currentEndTime;          // 当前显示结束时间
    int displayHours;                  // 当前显示的小时数
    QString currentDeviceName;         // 当前选择的设备名称

    // 显示模式相关
    ChartDisplayMode m_displayMode;  // 显示模式
    QString m_fixedDeviceId;         // 固定设备ID（窗口模式使用）
};

#endif  // CHARTWIDGET_H